package cn.yuanqiao.web.controller.archive.collection.appManager;


import cn.yuanqiao.archive.collection.appManager.domain.YqDataCategory;
import cn.yuanqiao.archive.collection.appManager.service.IYqDataCategoryService;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.web.page.TableDataInfo;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 料树类别
 *
 * @ProjectName: ruoyi-modules-system
 * @Package: cn.yuanqiao.web.controller.archive.appManager
 * @ClassName: YqDataCategoryController
 * @Author: lenovo
 * @Description: hbWang
 * @Date: 2023/7/28 14:28
 * @Version: 1.0
 */
@RestController
@RequestMapping("/archive/collection/data/category")
public class YqDataCategoryController extends BaseController {
    @Autowired
    private IYqDataCategoryService yqDataCategoryService;

    /**
     * 查询资料树类别列表
     */
    // @PreAuthorize("@ss.hasPermi('archive:dataCategory:list')")
    @GetMapping("/list")
    public TableDataInfo list(YqDataCategory yqDataCategory) {
        startPage();
        List<YqDataCategory> list = yqDataCategoryService.selectYqDataCategoryList(yqDataCategory);
        return getDataTable(list);
    }

    /**
     * 导出资料树类别列表
     */
    //  @PreAuthorize("@ss.hasPermi('archive:dataCategory:export')")
    @Log(title = "资料树类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YqDataCategory yqDataCategory) {
        List<YqDataCategory> list = yqDataCategoryService.selectYqDataCategoryList(yqDataCategory);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setNum(String.valueOf(i + 1));
        }
        ExcelUtil<YqDataCategory> util = new ExcelUtil<YqDataCategory>(YqDataCategory.class);
        util.exportExcel(response, list, "资料树类别数据");
    }

    /**
     * 资料书类别模板
     *
     * @param response
     */
    @Log(title = "资料书类别模板", businessType = BusinessType.EXPORT)
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<YqDataCategory> util = new ExcelUtil<YqDataCategory>(YqDataCategory.class);
        util.importTemplateExcel(response, "资料树类别模板");
    }

    /**
     * 导入资料类别数据
     *
     * @param file
     * @return
     * @throws Exception
     */
    @Log(title = "资料书类别模板", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        logger.info("导入start------");
        ExcelUtil<YqDataCategory> util = new ExcelUtil<>(YqDataCategory.class);
        List<YqDataCategory> yqDataCategories = util.importExcel(file.getInputStream());
        return AjaxResult.success(yqDataCategoryService.importData(yqDataCategories));
    }


    /**
     * 获取资料树类别详细信息
     */
    // @PreAuthorize("@ss.hasPermi('archive:dataCategory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(yqDataCategoryService.selectYqDataCategoryById(id));
    }

    /**
     * 根据资料书code获取类别列表
     *
     * @param treeCode
     * @return
     */
    @GetMapping(value = "/treeCode/{treeCode}")
    public TableDataInfo getInfoTree(@PathVariable("treeCode") String treeCode) {
        startPage();
        List<YqDataCategory> list = yqDataCategoryService.selectYqDataCategoryList(treeCode);
        return getDataTable(list);
    }

    /**
     * 根据资料树code获取类别代码
     *
     * @param treeCode
     * @return
     */
    @PostMapping(value = "/code")
    public AjaxResult getInfoCategoryCode(@RequestParam("treeCode") String treeCode) {
        List<YqDataCategory> list = yqDataCategoryService.selectYqDataCategoryList(treeCode);
        List<String> categoryCodeS = list.stream().map(m -> m.getCategoryCode()).collect(Collectors.toList());
        return success(categoryCodeS);
    }

    /**
     * 新增资料树类别
     */
    // @PreAuthorize("@ss.hasPermi('archive:dataCategory:add')")
    @Log(title = "资料树类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YqDataCategory yqDataCategory) {
        return toAjax(yqDataCategoryService.insertYqDataCategory(yqDataCategory));
    }

    /**
     * 修改资料树类别
     */
    //  @PreAuthorize("@ss.hasPermi('archive:dataCategory:edit')")
    @Log(title = "资料树类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YqDataCategory yqDataCategory) {
        return toAjax(yqDataCategoryService.updateYqDataCategory(yqDataCategory));
    }

    /**
     * 删除资料树类别
     */
    //   @PreAuthorize("@ss.hasPermi('archive:dataCategory:remove')")
    @Log(title = "资料树类别", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(yqDataCategoryService.deleteYqDataCategoryByIds(ids));
    }
}
